package cn.gong.jizhang.ui.base;

import java.util.Set;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.gong.jizhang.config.Configs;
import cn.gong.jizhang.datacenter.async.AsyncData.DBCallBack;
import cn.gong.jizhang.manager.ActivityManager;
import cn.gong.jizhang.service.MainService;
import cn.gong.jizhang.ui.R;
import cn.gong.jizhang.ui.adapter.BaseAdap;
import cn.gong.jizhang.utils.DebugUtils;

import com.baidu.mobstat.StatActivity;

public abstract class BaseAct extends StatActivity implements DBCallBack, OnClickListener {
	protected int actID;
	protected ActivityManager activityManager;
	protected BaseAdap adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 强制竖屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// 在ActivityManager中注册
		activityManager = ActivityManager.getInstance();
		activityManager.registerAct(this);
	}

	// 设置布局添加点击监听方法。
	protected void setLayout(int layoutResID) {
		setContentView(layoutResID);
		setClickListener();
	}

	// 所有点击监听归总
	protected abstract void setClickListener();

	// 设置点击事件监听
	protected void setClick(int viewID) {
		findViewById(viewID).setOnClickListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		// 保存当前act
		ActivityManager.CURRENT_ACTID = actID;
	}

	@Override
	public void onResume() {
		super.onResume();
		// 恢复暂时储存的数据
		recoveryDate();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	public void onPause() {
		super.onPause();
		// 清理并暂时缓存数据
		clearDate();
	}

	// 清理并暂时缓存数据
	protected abstract void clearDate();

	// 恢复暂时储存的数据
	protected abstract void recoveryDate();

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 移除act
		activityManager.removeActivity(this);
		// 清理用到的适配器
		if (adapter != null) {
			adapter.destory();
			adapter = null;
		}
	}

	protected void toast(final String msg) {
		// 主线程执行toast
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(BaseAct.this, msg, Configs.TOAST_TIME).show();
			}
		});
	}

	// 一些启动和关闭act的方法
	protected void openActForNew(Intent intent) {
		startActivity(intent);
		animationForNew();
	}

	protected void openAct(Intent intent) {
		startActivity(intent);
	}

	protected void closeAct() {
		finish();
	}

	protected void closeActForOld() {
		finish();
		animationForOld();
	}

	public void animationForNew() {
		overridePendingTransition(R.anim.translatex100to0, R.anim.translatex0tof100);
	}

	public void animationForOld() {
		overridePendingTransition(R.anim.translatexf100to0, R.anim.translatex0to100);
	}

	// 返回按钮默认事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			closeActForOld();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	// 退出程序方法
	public void alertExit() {
		AlertDialog.Builder ab = new AlertDialog.Builder(this);
		ab.setTitle("提示");
		ab.setMessage("确定退出吗?");
		ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent it = new Intent(BaseAct.this, MainService.class);
				getApplicationContext().stopService(it);

				ActivityManager manager = ActivityManager.getInstance();
				Set<Integer> keySet = manager.getActivityKeySet();
				for (Integer key : keySet) {
					manager.getActivity(key).finish();
				}

				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		ab.setNegativeButton("取消", null);
		ab.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				}
				return false;
			}
		});
		ab.create().show();
	}

	// 一些获取View的方法
	protected LinearLayout findLinearLayoutById(int id) {
		return (LinearLayout) findViewById(id);
	}

	protected LinearLayout findLinearLayoutById(int id, View view) {
		return (LinearLayout) view.findViewById(id);
	}

	protected ListView findListViewById(int id) {
		return (ListView) findViewById(id);
	}

	protected ListView findListViewById(int id, View view) {
		return (ListView) view.findViewById(id);
	}

	protected Button findButtonById(int id) {
		return (Button) findViewById(id);
	}

	protected Button findButtonById(int id, View view) {
		return (Button) view.findViewById(id);
	}

	protected ImageView findImageViewById(int id) {
		return (ImageView) findViewById(id);
	}

	protected ImageView findImageViewById(int id, View view) {
		return (ImageView) view.findViewById(id);
	}

	protected RelativeLayout findRelativeLayoutById(int id) {
		return (RelativeLayout) findViewById(id);
	}

	protected RelativeLayout findRelativeLayoutById(int id, View view) {
		return (RelativeLayout) view.findViewById(id);
	}

	protected TextView findTextViewById(int id) {
		return (TextView) findViewById(id);
	}

	protected TextView findTextViewById(int id, View view) {
		return (TextView) view.findViewById(id);
	}

	protected CheckBox findCheckBoxById(int id) {
		return (CheckBox) findViewById(id);
	}

	protected CheckBox findCheckBoxById(int id, View view) {
		return (CheckBox) view.findViewById(id);
	}

	protected FrameLayout findFrameLayoutById(int id) {
		return (FrameLayout) findViewById(id);
	}

	protected FrameLayout findFrameLayoutById(int id, View view) {
		return (FrameLayout) view.findViewById(id);
	}

	protected ImageButton findImageButtonById(int id) {
		return (ImageButton) findViewById(id);
	}

	protected ImageButton findImageButtonById(int id, View view) {
		return (ImageButton) view.findViewById(id);
	}

	protected EditText findEditTextById(int id) {
		return (EditText) findViewById(id);
	}

	protected EditText findEditTextById(int id, View view) {
		return (EditText) view.findViewById(id);
	}

	// 一些打印信息的方法
	protected void debug(int msg) {
		debug(String.valueOf(msg));
	}

	protected void debug(long msg) {
		debug(String.valueOf(msg));
	}

	protected void debug(String msg) {
		DebugUtils.debug(msg, getClass());
	}

	protected void debug(String name, float msg) {
		debug(name, String.valueOf(msg));
	}

	protected void debug(String name, int msg) {
		debug(name, String.valueOf(msg));
	}

	protected void debug(String name, long msg) {
		debug(name, String.valueOf(msg));
	}

	protected void debug(String name, String msg) {
		DebugUtils.debug(name + ":" + msg, getClass());
	}

	protected void debug(String tag, String name, String msg) {
		DebugUtils.debug(tag, name + ":" + msg, getClass());
	}

	protected void error(Throwable e) {
		DebugUtils.error(e);
	}

	protected void exception(Exception e) {
		DebugUtils.exception(e);
	}

	protected void isNull(String name, Object obj) {
		DebugUtils.isNull(name, obj, getClass());
	}

	public int getActID() {
		return actID;
	}

	public void setActID(int actID) {
		this.actID = actID;
	}
}
